package membe

import (
	"github.com/gin-gonic/gin"
	"github.com/vueadmin/app/api/internal/dto"
	"github.com/vueadmin/app/api/internal/logic"
	"github.com/vueadmin/app/api/internal/svc"
	"github.com/vueadmin/utils"
	"github.com/vueadmin/utils/common/request"
	"github.com/vueadmin/utils/common/result"
	"github.com/vueadmin/utils/conv"
	"github.com/vueadmin/utils/response"
)

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func GetPageList(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembePageReq{
		PageReq:request.PageReq{Page: 1,Limit: 20},
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	list, count, err := l.GetPageList(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", result.M{
		"data":  list,
		"total": count,
	})
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func Update(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeUpdateReq{
		MembeId : conv.Int(c.Value("membe_id")),
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Update(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func Delete(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeDeleteReq{
		MembeId : []int{conv.Int(c.Value("membe_id"))},
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.Delete(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "删除成功", nil)
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func Detail(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeDetailReq{
		MembeId : conv.Int(c.Value("membe_id")),
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	info,err := l.Detail(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", info)
}

/**
 * @description(重置密码)
 * @buildcode(true)
*/
func ResetPwd(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeResetPwdReq{
		MembeId : conv.Int(c.Value("membe_id")),
	}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	if err := l.ResetPwd(&req); err != nil{
		response.Err(c, err)
		return
	}
	response.Success(c, "修改成功", nil)
}

/**
 * @description(用户登录)
 * @buildcode(true)
*/
func Login(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeLoginReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	token,err := l.Login(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", token)
}

/**
 * @description(短信验证码登录)
 * @buildcode(true)
*/
func Loginsms(c *gin.Context, svcCtx *svc.ServiceContext) {
	l := logic.NewMembeLogic(c,svcCtx)
	token,err := l.Loginsms(c.Value("mobile").(string))
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "返回成功", token)
}

/**
 * @description(发送验证码)
 * @buildcode(true)
*/
func SendSms(c *gin.Context, svcCtx *svc.ServiceContext) {
	req := dto.MembeSendSmsReq{}
	if err := utils.BindAndValid(c, &req); err != nil {
		response.Fail(c, err.Error())
		return
	}
	l := logic.NewMembeLogic(c,svcCtx)
	data,err := l.SendSms(&req)
	if err != nil {
		response.Err(c, err)
		return
	}
	response.Success(c, "发送成功", data)
}

